/**
 * Created by Administrator on 2017/3/13 0013.
 */
var resource;
var pixiStageW=740;
var pixiStageH=1136;
var renderer;
var stage;

function PSP(imgName){
    // console.log("images/"+imgName+".png");
    return new PIXI.Sprite(resource["images/"+imgName+".png"].texture);
}


Main={
    Create:function(){
        var main={};
        stage = new PIXI.Container()
        var CC;
            renderer = PIXI.autoDetectRenderer(256, 256,{antialias:true,transparent:true,resolution:1});
        renderer.view.style.border = "0px dashed black";
        renderer.resize(666,666);
        renderer.backgroundColor=0xffffff;
        document.body.appendChild(renderer.view);


        //音乐加载
        var musicControl=MusicControl.Create();
        // musicControl.startMusic();

        //资源加载
        var hajime=Hajime.Create();
        document.addEventListener('HajimeCompleted',HajimeCompleted);
        function HajimeCompleted(e){
            resource=e.detail;
            setup();
        }

        CC=ControlCenter.Create();
        stage.addChild(CC.ReturnWhole());

        //加载页面在正式页面之上
        stage.addChild(hajime.ReturnWhole());


        //页面适配
        resizeHandler();
        window.onresize = resizeHandler;
        function resizeHandler() {
            renderer.resize(window.innerWidth, window.innerHeight);
            mode='noBorder';
            switch (mode) {
                case 'exactFit':
                    stage.scale.x = window.innerWidth / pixiStageW;
                    stage.scale.y = window.innerHeight / pixiStageH;
                    break;
                case 'noBorder':
                    stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerWidth / pixiStageW) : (window.innerHeight / pixiStageH);
                    stage.scale.y = stage.scale.x;
                    break;
                case 'noScale':
                    stage.scale.x = 1;
                    stage.scale.y = 1;
                    break;
                case 'showAll':
                    stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerHeight / pixiStageH) : (window.innerWidth / pixiStageW);
                    stage.scale.y = stage.scale.x;
                    break;
            }
            stage.x = (window.innerWidth - pixiStageW * stage.scale.x) * .5;
            stage.y = (window.innerHeight - pixiStageH * stage.scale.y) * .5;
        };


        var bl_setup=false;


        //程序开始
        function setup(){
            console.log("Start!");
            //控制页面
            // CC=ControlCenter.Create();
            // stage.addChild(CC.ReturnWhole());
            CC.Go() ;
            bl_setup=true;
            //加载页面在正式页面之上
            // stage.addChild(hajime.ReturnWhole());
        };









        //渲染层
        gameLoop();
        function gameLoop(){

            if(bl_setup) {
                CC.DoIt();
            }

            //Tween必须
            TWEEN.update();

            //Render The Stage
            requestAnimationFrame(gameLoop);
            renderer.render(stage);
        }
        return main;
    }
}
